import { Component, OnInit, TemplateRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DictDto } from 'src/app/models/sys/dict.dto';
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { ApiService } from 'src/app/services/api.service';
import { PageParam } from 'src/app/models/param/page.param';
import { Consts } from 'src/app/consts/consts';
import { NzTableQueryParams } from 'ng-zorro-antd/table';
import { GroupDto } from 'src/app/models/sys/group.dto';
import { BehaviorSubject } from 'rxjs';
import { EditMode } from 'src/app/models/enums/edit-mode.enum';


@Component({
  selector: 'app-group-type',
  templateUrl: './group-type.component.html',
  styleUrl: './group-type.component.less'
})
export class GroupTypeComponent implements OnInit {
  modeSubject = new BehaviorSubject<{ mode: EditMode, groupType?: DictDto }>({mode: EditMode.ADD, groupType: undefined});

  pageIndex = 1;
  pageSize = 15;
  total = 0;

  groupTypes: DictDto[] = [];

   // 对话框title
   editTitle?: string;
   typeName?: string;

     // 详情
  typeSubject = new BehaviorSubject<DictDto>(new DictDto());

  constructor(private _api: ApiService,
    private _fb: UntypedFormBuilder,
    private _modal: NzModalService,
    private _message: NzMessageService) {
  }
  ngOnInit(): void {
    this.onSearch();
  }

  onSearch() {
    const param = new PageParam();
    param.pageNum = this.pageIndex;
    param.pageSize = this.pageSize;

    this._api.getGroupTypeList(param).subscribe((resp) => {
      if (resp.code == Consts.RESP_OK && resp.data) {
        this.groupTypes = resp.data;
        this.total = resp.total;
      }
    });
  }

  onCreate(tplTitle: TemplateRef<object>, tplContent: TemplateRef<object>) {
    this.editTitle = '创建组类型';
    this.typeName = undefined;

    this._modal.create({
      nzTitle: tplTitle,
      nzContent: tplContent,
      nzFooter: null,
      nzMaskClosable: false,
      nzClosable: false,
      nzWidth: 800,
      nzOnOk: () => {
        console.log('groupType added');
      }
    });

    this.modeSubject.next({mode: EditMode.ADD, groupType: undefined});
  }

  onUpdate(groupType: DictDto, tplTitle: TemplateRef<object>, tplContent: TemplateRef<object>) {
    this.editTitle = '编辑组类型';
    this.typeName = groupType.display;

    this._modal.create({
      nzTitle: tplTitle,
      nzContent: tplContent,
      nzFooter: null,
      nzMaskClosable: false,
      nzClosable: false,
      nzWidth: 800,
      nzOnOk: () => {
        console.log('groupType added');
      }
    });

    this.modeSubject.next({mode: EditMode.EDIT, groupType: groupType});
  }

  onDetail(tplTitle: TemplateRef<object>, tplContent: TemplateRef<object>, groupType: DictDto) {
    this.editTitle = '组类型详情';

    this._modal.create({
      nzTitle: tplTitle,
      nzContent: tplContent,
      nzFooter: null,
      nzMaskClosable: false,
      nzClosable: false,
      nzWidth: 800,
      nzOnOk: () => {
        console.log('groupType detail');
      }
    });

    this.typeSubject.next(groupType);
  }

  handleDelete(groupKey: number) {
    this._api.deleteGroupType(groupKey).subscribe((resp) => {
      if (resp.code == Consts.RESP_OK) {
        this._message.info("删除组类型成功");
        this.onSearch();
      } else {
        this._message.warning("删除组类型失败 " + resp.message);
      }
    });
   }

  onPageParamChange(params: NzTableQueryParams) {
    this.pageIndex = params.pageIndex;
    this.pageSize = params.pageSize;
    this.onSearch();
  }

  onAddGroupTypeEvent(result: boolean) {
    if (result) {
      this.onSearch();
    }

    this._modal.closeAll();
  }

  onDetailEvent(result: boolean) {
    this._modal.closeAll();
    console.log(`${result}`);
  }
}
